<?php

class User extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'users':
	 * @var integer $id
	 * @var string $name
	 * @var string $username
	 * @var string $password
	 * @var integer $is_active
     * @var integer $user_groups_id
     * @var integer $cities_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'df_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name','length','max'=>45),
			array('username','length','max'=>45),
			array('password','length','max'=>45)
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'group' => array(self::BELONGS_TO, 'Group', 'user_groups_id'),
    		'city' => array(self::BELONGS_TO, 'City', 'cities_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'name' => 'Name',
			'user_groups_id' => 'Group',
			'username' => 'Username',
			'password' => 'Password',
			'cities_id' => 'City',
		);
	}
    
    // Returns list of searchable fileds for DataFilter widget
    public function getDataFilterSearchFields($filterName)
    {
        switch ($filterName) {
            case 'userFieldsSearch': //filter name
                return array(
                    'df_users.id'=>'User ID', //field name => display name
                    'df_users.name'=>'Name',
                    'df_users.username'=>'Username',
                );
        }
    }
   
    // Applies search criteria enterd using DataFilter widget
    public function applyDataSearchCriteria(&$criteria, $filterName, $searchField, $searchValue)
    {
        if($filterName == 'userFieldsSearch') {
            $localCriteria = new CDbCriteria;
            //$localCriteria->condition = ' '.$searchField.' LIKE "%'.$searchValue.'%" ';
            $localCriteria->condition = ' '.$searchField.' LIKE :searchValue ';
            $localCriteria->params = array(':searchValue'=>'%'.$searchValue.'%');
            $criteria->mergeWith($localCriteria);
        }        
    }

    // Returns options for DataFilter widget
    public function getDataFilterOptions($filterName)
    {
        switch ($filterName) {
            case 'Group':  //filter name
            case 'groupFilter2':
                // data from database
                $groups = Group::model()->findAll();
                return CHtml::listData($groups, 'id', 'name');
            case 'Country':
            case 'countryFilter2':
                $countries = Country::model()->findAll();
                return CHtml::listData($countries, 'id', 'name');
            case 'City':
                $criteria = new CDbCriteria;
                $country = Yii::app()->request->getParam('countryFilter');
                // city filter depends from country filter
                if (isset($country) && !empty($country)) {
                    $criteria->condition = ' countries_id = :country';
                    $criteria->params = array(':country'=>$country);
                }
                $cities = City::model()->findAll($criteria);
                return CHtml::listData($cities, 'id', 'name');
           case 'activeDropFilter':
                // static data (not from database)
                $options = array(
                    array('id'=>'', 'name'=>'All'),
                    array('id'=>0, 'name'=>'Not active'),
                    array('id'=>1, 'name'=>'Active'),
                );
                return CHtml::listData($options, 'id', 'name');
        }      
    }

    // Applies filter criteria enterd using DataFilter widget
    public function applyDataFilterCriteria(&$criteria, $filterName, $filterValue)
    {
        if($filterName == 'Group' || $filterName == 'groupFilter2') {
            $localCriteria = new CDbCriteria;
            CDataFilter::setCondition('user_groups_id', $filterValue, $localCriteria);
            $criteria->mergeWith($localCriteria);
        }

        if($filterName == 'Country' || $filterName == 'countryFilter2') {
            $localCriteria = new CDbCriteria;
            //'null' value is a spectial option for coutryFilter
            if ($filterValue != 'null') {
                $localCriteria->select = 'df_users.*';
                $localCriteria->join =
                    'INNER JOIN `df_cities` cities
                    ON (`df_users`.`cities_id`=cities.`id`)
                    AND (cities.countries_id = :countryID) ';
                $localCriteria->params = array(':countryID'=>$filterValue);
                //$localCriteria->group = ' df_users.id ';
            } else {
                $localCriteria->condition = ' cities_id is null ';
            }
            $criteria->mergeWith($localCriteria);
        }

        if($filterName == 'City') {
            $localCriteria = new CDbCriteria;
            CDataFilter::setCondition('cities_id', $filterValue, $localCriteria);
            $criteria->mergeWith($localCriteria);
        }

        if($filterName == 'activeFilter') {
            if ($filterValue !== 'active') return;
            $localCriteria = new CDbCriteria;
            CDataFilter::setCondition('is_active', 1, $localCriteria);
            $criteria->mergeWith($localCriteria);
        }

        if($filterName == 'activeDropFilter') {            
            $localCriteria = new CDbCriteria;
            CDataFilter::setCondition('is_active', $filterValue, $localCriteria);
            $criteria->mergeWith($localCriteria);
        }

    }

}